package com.mcptt.db.model;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.DatabaseUtils;

import com.cloudptt.api.comm.CheckData;
import com.cloudptt.api.product.log.Log;
import com.mcptt.db.DbConst;

import java.io.Serializable;

public class SessionReadStatus extends AddrInfo implements Serializable,Comparable<SessionReadStatus>
{
	private String conversationId = null;
	private String msgId = null;

	private String groupId = "";
	private int status = 1;

	public SessionReadStatus(String conversationId,String msgId,String groupId,String addrId,int status)
	{
		super(UserType_User,"User_"+addrId,addrId,0);
		this.conversationId = conversationId;
		this.msgId = msgId;

		this.groupId = groupId;
		this.status = status;
	}



	public SessionReadStatus(Cursor c)
	{
		ContentValues args = new ContentValues();
		DatabaseUtils.cursorRowToContentValues(c, args);
		createFromContentValue(args);
	}

//	public SessionReadStatus() {
//		this.addrId = "";
//		this.name = "";
//
//	}

	public boolean isSameOne(AddrInfo oldInfo)
	{
		if(oldInfo == null)
		{
			return false;
		}
		String oldMcId = oldInfo.addrId;
		if(this.addrId.equals(oldMcId))
		{
			return true;
		}
		return false;
	}

	public boolean isSameOne(String oldNum)
	{
		if(this.addrId.equals(oldNum))
		{
			return true;
		}
		return false;
	}

	/**
	 * Pack the object content value to store
	 * 
	 * @return The content value representing the message
	 */
	public ContentValues getContentValues() {
		ContentValues cv = new ContentValues();
		cv.put(DbConst.COL_ConversationId, conversationId);
		cv.put(DbConst.COL_MsgId, msgId);

		cv.put(DbConst.COL_GroupId, groupId);
		cv.put(DbConst.COL_AddrId, addrId);

		cv.put(DbConst.COL_Status, status);

		return cv;
	}

	public final void createFromContentValue(ContentValues args) {

		Log.debug(this.getClass(), "createFromContentValue");
		String tmp_s;
		tmp_s = args.getAsString(DbConst.COL_ConversationId);
		if(tmp_s != null) {
			conversationId = tmp_s;
		}
		tmp_s = args.getAsString(DbConst.COL_MsgId);
		if(tmp_s != null) {
			msgId = tmp_s;
		}

		tmp_s = args.getAsString(DbConst.COL_Type);
		if(tmp_s != null) {
			type = CheckData.parseInt(tmp_s);
		}
		tmp_s = args.getAsString(DbConst.COL_AddrId);
		if(tmp_s != null) {
			addrId = tmp_s;
		}
		tmp_s = args.getAsString(DbConst.COL_Name);
		if(tmp_s != null) {
			name = tmp_s;
		}
		tmp_s = args.getAsString(DbConst.COL_Attr);
		if(tmp_s != null) {
			int value = CheckData.parseInt(tmp_s);
			if(value != -1)
			{
				attr = value;
			}
		}
		tmp_s = args.getAsString(DbConst.COL_GroupId);
		if(tmp_s != null) {
			groupId = tmp_s;
		}
		tmp_s = args.getAsString(DbConst.COL_Status);
		if(tmp_s != null) {
			status = CheckData.parseInt(tmp_s);
		}
	}

	public String getConversationId() {
		return conversationId;
	}

	public String getMsgId() {
		return msgId;
	}


	public String getGroupId() {
		return groupId;
	}

	public int getStatus() {
		return status;
	}
	public String getDscp()
	{
		if(name != null
				&& name.length() > 0)
		{
			return name;
		}
		if(addrId != null
				&& addrId.length() > 0)
		{
			return addrId;
		}
		else
		{
			return "";
		}
	}
	public void setStatus(int status) {
		this.status = status;
	}

	public String toString()
	{
		StringBuffer strBuf = new StringBuffer();
		strBuf.append("conversationId = ").append(conversationId).append(",");
		strBuf.append("msgId = ").append(msgId).append(",");
		strBuf.append("type = ").append(type).append(",");
		strBuf.append("addrId = ").append(addrId).append(",");
		strBuf.append("groupId = ").append(groupId).append(",");
		strBuf.append("name = ").append(name).append(",");
		strBuf.append("attr = ").append(attr).append(",");
		strBuf.append("status = ").append(status).append(".");

		return strBuf.toString();
	}

	@Override
	public int compareTo(SessionReadStatus sessionReadStatus) {
		return sessionReadStatus.status - status;
	}
}
